package com.eras.shoes.utils;

import android.util.Log;

import com.alibaba.fastjson.JSONArray;
import com.eras.shoes.contents.BleDataEx;
import com.eras.shoes.contents.PressureTemperatureData;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ZeroClearingManage {

    private List<BleDataEx.BleData> lList_1 = new ArrayList<>();//左脚清零列表
    private List<BleDataEx.BleData> rList_1 = new ArrayList<>();//右脚清零列表

    public void clearLeft(){
        lList_1.clear();
    }
    public void clearRight(){
        rList_1.clear();
    }



    /**
     * 添加左脚清零列表
     * @param bleData
     */
    public void setLeftData(BleDataEx.BleData bleData){
        if (lList_1.size() > 30) {
            lList_1.clear();
        }
        lList_1.add(bleData);
    }

    /**
     * 添加右脚清零列表
     * @param bleData
     */
    public void setRightData(BleDataEx.BleData bleData){
        if (rList_1.size() > 30) {
            rList_1.clear();
        }
        rList_1.add(bleData);
    }



    /**
     * 获取左脚清零数据
     * @return
     */
    public PressureTemperatureData  getLeftData(){
        Double lZeroClearing = 0.0;
        Float temperature = 0.0f;
        if (lList_1.size() >= 5){
            //左脚清零
            lZeroClearing = getPressureVal(lList_1,"");
            temperature = getTemperatureVal(lList_1);
            lList_1.clear();
        }
        return new PressureTemperatureData(lZeroClearing, temperature);
    }



    private Float getTemperatureVal(List<BleDataEx.BleData> list) {

        Float temp = 0.0f;
        try {
                if(list != null && !list.isEmpty()){
                    temp = list.get(0).dataList.get(27);//温度
                }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return temp;
    }

    /**
     * 获取右脚清零数据
     * @return
     */
    public Double getRightData(){
        Double rZeroClearing = 0.0;
        if (rList_1.size() >= 5){
            //右脚清零
            rZeroClearing = getPressureVal(rList_1,"right");
            rList_1.clear();
        }
        return rZeroClearing;
    }

    /**
     * 获取校准结果
     * @return
     */
//    private CalibrationObject getCalibrationObject(){
//        //左脚清零
//        Double lZeroClearing = getLeftData();
//        //右脚清零
//        Double rZeroClearing = getRightData();
//        return new CalibrationObject(lZeroClearing, rZeroClearing, 0);
//    }

    private Double getPressureVal(List<BleDataEx.BleData> list,String foot){

        List<Float> floats = new ArrayList<>();
        try {
            for (BleDataEx.BleData item : list) {
                floats.add(item.dataList.get(28));//压力
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        Log.i("ZeroClearingManage", foot +"  mondifyFlowStatus "+ JSONArray.toJSONString(floats));
//        Collections.sort(floats);
        Collections.sort(floats, (f1, f2) -> Float.compare(f2, f1));
        float pressureData = 0.0f;
        double doubleValue =0.0;
//        doubleValue = floats.get(0);
        List<Float> floatList = floats.subList(0, 5);
//        Log.i("ZeroClearingManage", foot +"  floatList= "+ floatList);
        doubleValue = floatList.get(4);
//        float v = floatList.get(4) - floatList.get(0);
//        if (v <= Float.parseFloat("3")) {
//            for (Float i : floatList) {
//                pressureData = pressureData + i;
//            }
//        Log.i("ZeroClearingManage", foot +"  pressureData= "+ pressureData);
//            doubleValue = new BigDecimal(pressureData).divide(new BigDecimal(5), 4, RoundingMode.CEILING).doubleValue();
//        }
        Log.i("ZeroClearingManage", foot +"  doubleValue= "+ doubleValue);
        return doubleValue;
    }

    public static String formatWithStringFormatFloat(float value) {
        return String.format("%.2f", value);
    }
    public static String formatWithStringFormat(double value) {
        return String.format("%.2f", value);
    }
    public static String formatWithStringFormatOne(double value) {
        return String.format("%.1f", value);
    }

}
